import axios from "axios";
// import qs from "qs";
import router from "@/router";
import {ElMessage} from "element-plus";

const sysMapping = require("./sysMapping");

// axios.defaults.baseURL = 'http://www.wenbaizhi.top:18081'  //正式
axios.defaults.baseURL = sysMapping.BACKEND_SOCKET  //正式
// axios.defaults.baseURL = 'http://test' //测试

//post请求头
// axios.defaults.headers.post["Content-Type"] ="application/x-www-form-urlencoded;charset=UTF-8";
//设置超时
axios.defaults.timeout = 10000;

axios.interceptors.request.use(
    config => {

        const token = localStorage.getItem(sysMapping.TOKEN);
        config.headers[sysMapping.TOKEN] = token;
        // "Content-Type":"application/json"
        if (config.headers["Content-Type"] !== "multipart/form-data" ) {
            config.headers["Content-Type"] = "application/json";
        }

        config.headers["Access-Control-Allow-Origin"] = "*";
        // console.log(config);
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

axios.interceptors.response.use(
    response => {
        if (response.status == 200) {
            return Promise.resolve(response);
        } else {
            return Promise.reject(response);
        }
    },
    error => {
        ElMessage({
            showClose: true,
            message: `异常请求：${JSON.stringify(error.message)}`,
            type: "error",
            center: true,
        });
    }
);
// axios.interceptors.request.use(
//     (config) => {
//         let token = sessionStorage.getItem('token')//这个是登录时存进本地环境的token
//         const sysMapping = require("sysMapping")
//         config.headers[sysMapping.TOKEN] = localStorage.getItem(sysMapping.TOKEN)
//         return config
//     })

// 添加响应拦截器
axios.interceptors.response.use(function (response) {

    if (response.data.status == 30001) {
        console.log("axios检测到你没登陆response.data.status == 30001");
        // alert("登录失效，请重新登录");
        ElMessage({
            showClose: true,
            message: "Token失效，请重新登录",
            type: "error",
            center: true,
        });
        console.log(response.request)
        router.push("/user/login");
    }
    else if (response.data.status == 500) {
        ElMessage({
            showClose: true,
            message: "出错："+response.data.message,
            type: "error",
            center: true,
        });
    }
    // 对响应数据做点什么
    return response
}, function (error) {
    // 对响应错误做点什么
    if (error.response) {
        switch (error.response.status) {
            case 30001:
                // store.commit('del_token')
                // router.push('/login')
                // console.log("axios检测到你没登陆")
        }
    }
    return Promise.reject(error)
})


export default {
    post(url, data) {
        return new Promise((resolve, reject) => {

            axios({
                method: 'post',
                url,
                // data: qs.stringify(data),
                // data: new URLSearchParams(data),
                data: JSON.stringify(data),
            })
                .then(res => {
                    resolve(res.data.data)
                })
                .catch(err => {
                    reject(err)
                });
        })
    },
    get(url, data) {
        return new Promise((resolve, reject) => {
            // console.log(qs.stringify(data));
            // const tokenKey = sysMapping.TOKEN;
            // const token = localStorage.getItem(sysMapping.TOKEN);
            // config.headers[sysMapping.TOKEN] = token;
            axios({
                method: 'get',
                url,
                // params: data,
                params: data,

            })
                .then(res => {
                    resolve(res.data.data)
                })
                .catch(err => {
                    reject(err)
                })
        })
    },
    postImage(url, fileData) {

        return new Promise((resolve, reject) => {
            axios.post(url, fileData, {
                headers: {
                    'Content-Type': 'multipart/form-data'
                }
            })
                .then(res => {
                    resolve(res.data.data)
                })
                .catch(err => {
                    reject(err)
                })
        });

    },
};

